<template>
  <page-dialog
    :title="dialogInfo.title"
    v-model:visible="shows"
    :width="dialogInfo.width"
    :bt-loading="dialogInfo.btLoading"
    :bt-list="dialogInfo.btList"
  >
    <div class="dialog-table">
      <NewPageFilter
        v-model:query="filterInfo.query"
        :filter-list="filterInfo.searchBtn"
      >
        <template v-slot:filter-button>
          <div class="dialog-table-header">
            <div class="header-left">
              <div class="lable">产品名称：{{ info.commonName }}</div>
              <div class="lable">生产批号：{{ info.batchNumber }}</div>
            </div>
            <div class="header-right">
              <div class="scan-groups">
                <el-input
                  v-model="filterInfo.keyword"
                  :clearable="true"
                  placeholder="请输入个体码"
                  @keyup.enter.native="search"
                  @input="(e) => (filterInfo.keyword = validSe(e))"
                >
                </el-input>
                <el-button style="margin-left: 10px" @click="search"
                  >搜索</el-button
                >
              </div>
            </div>
          </div>
        </template>
      </NewPageFilter>
    </div>

    <dialog-table
      :refresh="tableInfo.refresh"
      :init-curpage="tableInfo.initCurpage"
      v-model:data="tableInfo.data"
      :api="tableInfo.api"
      :check-box="false"
      :pager="false"
      :tab-index="true"
      :handle="tableInfo.handle"
      :listen-height="false"
      :query="filterInfo.query"
      :field-list="tableInfo.fieldList"
      @handleEvent="handleEvent"
    >
    </dialog-table>
    <chose-printer :visible="printerInfo.visible" @callBack="printerCallBack" />
  </page-dialog>
</template>

<script setup name="IndividualDetails">
import { getCustomizeSetting } from "@/api/customize";
import DialogTable from "@/components/DialogTable/index";
import {
  getWsmStoreStockOrderDetailCustomizeList,
  WmsStoreOrderDetailsPrintQrCodeVO,
} from "@/api/wms";

const emits = defineEmits();
const { proxy } = getCurrentInstance();
const props = defineProps({
  api: {
    type: Function,
    default: getWsmStoreStockOrderDetailCustomizeList,
  },
  showDialog: {
    type: Boolean,
  },
  pageStatus: {
    type: Boolean,
  },
  query: {
    type: Object,
    default: function () {
      return {};
    },
  },
  info: {
    type: Object,
    default: function () {
      return {};
    },
  },
});

// 过滤相关配置
const filterInfo = ref({
  keyword: "",
  query: {
    queryMap: {
      ...props.query,
    },
    queryParams: {},
    customizeUserVO: {},
  },
  searchBtn: [{ slot: true, value: "button" }],
});
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  api: undefined,
  copyData: [],
  selections: [],
  fieldList: [],
  handle: {
    fixed: "right",
    label: "操作",
    width: '80',
    btList: [
      {
        label: "打印二维码",
        event: printSingleQrCode,
        show: proxy.$permission("wmsAccountStock:printCode") && props.pageStatus,
      },
    ],
  },
});
// 弹窗相关
const dialogInfo = ref({
  title: "个体详情",
  width: "90%",
  btLoading: false,
  btList: [{ label: "关闭", event: handleClose, show: true }],
});
const printerInfo = ref({
  visible: false,
  data: {},
});
const selectRow = ref("");

const shows = computed({
  get() {
    return props.showDialog;
  },
  set(newValue) {
    emits("update:showDialog", newValue); // 更新父组件shows
  },
});

let obj = {
  price: {
    label: "单价",
    value: "price",
    formatter: proxy.$fn.TripartiteMethod,
    permission: !proxy.$permission("wmsAccountStock:price"),
    show: false,
    hidden: true,
  },
  sumAmount: {
    label: "总金额",
    value: "sumAmount",
    formatter: proxy.$fn.TripartiteMethod,
    permission: !proxy.$permission("wmsAccountStock:price"),
    show: false,
    hidden: true,
  },
  defaultSupplierPrice: {
    label: "默认供应商价格",
    value: "defaultSupplierPrice",
    type: "column",
    permission: !proxy.$permission("wmsAccountStock:price"),
    formatter: proxy.$fn.TripartiteMethod,
    hidden: true,
  },
};
tableInfo.value.fieldList = Object.values({
  ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
  ...obj,
});
getCustomize();

// 获取列表
function getList() {
  tableInfo.value.refresh = Math.random();
}
function search() {
  const search = filterInfo.value.keyword;
  if (search) {
    tableInfo.value.data = tableInfo.value.copyData.filter((data) => {
      return Object.keys(data).some((key) => {
        if (key === "qrCode") {
          return String(data[key]).indexOf(search) > -1;
        } else if (key === "uniqueCode") {
          return String(data[key]).indexOf(search) > -1;
        } else if (key === "factorySubCode") {
          return String(data[key]).indexOf(search) > -1;
        } else if (key === "factoryMainCode") {
          return String(data[key]).indexOf(search) > -1;
        }
      });
    });
  } else {
    tableInfo.value.data = tableInfo.value.copyData;
  }
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false;
  emits("reset");
}
function handleEvent(event, data) {
  switch (event) {
    case "list":
      tableInfo.value.copyData = data;
      break;
  }
}
//打印单个二维码
function printSingleQrCode(row = undefined) {
  if (!row.qrCode) {
    proxy.$message({
      message: "没有生成二维码，不能打印",
      type: "warning",
      duration: 3500,
      showClose: true,
    });
    return;
  }
  let ids = [row.id];
  if (ids.length > 0) {
    WmsStoreOrderDetailsPrintQrCodeVO(ids).then((resp) => {
      if (resp && resp.success) {
        printerInfo.value.visible = true;
        printerInfo.value.data = resp.data;
      }
    });
  }
}
function printerCallBack(printer) {
  if (printer !== false) {
    proxy.$print.buildRenderBarCode(printerInfo.value.data, printer);
  }
  printerInfo.value.visible = false;
}
function getCustomize() {
  let param = {
    tableKey:
      "wmsStoreOrderIndividualDetails-com.slip.spd.entity.vo.wms.WmsStoreStockOrderDetailVO",
  };
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data;
      filterInfo.value.query.customizeUserVO = res.data;
      tableInfo.value.fieldList = proxy.$fn.setFieldList(
        tableInfo.value.fieldList,
        data.detail
      );
      tableInfo.value.api = props.api;
      getList();
    }
  });
}
</script>

<style scoped lang="scss">
:deep(.filter-item) {
  width: 100%;
}
.text-style {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.scan-groups {
  display: flex !important;
  align-items: center;
}
</style>
